package com.zhiche.lisa.bms.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: liuanshun
 * @Description:
 * @Date: Create in 11:07 2018/11/23
 */
@Component
public class UacUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(UacUtil.class);

    @Value("${lisa.uac.url}")
    private String url;

    @Value("${lisa.uac.defaultToken}")
    private String defaultToken;

    @Value("${lisa.uac.authForm}")
    private String authForm;

    @Value("${lisa.uac.socketTimeout}")
    private Integer socketTimeout;

    @Autowired
    private InterfaceLogUtil interfaceLogUtil;

    public String getToken(String userName, String pwd) {
        String returnVal = "";
//        InterfaceLog interfaceLog = new InterfaceLog();
//        try {
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", defaultToken));

        Map<String, String> params = new HashMap<>();
        params.put("username", userName);
        params.put("password", pwd);
//            interfaceLogUtil.setCallerPartyCommonLog(interfaceLog, params, url + authForm);

        String strReturn = HttpClientUtil.post(url + authForm, headNamePairs, params, socketTimeout);
//            interfaceLogUtil.setCallPartySuccessLog(interfaceLog,strReturn);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<String> restfulResponse =
                    JSON.parseObject(strReturn, new TypeReference<RestfulResponse<String>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                JSONObject jsonObject = JSONObject.parseObject(restfulResponse.getData());
//                    interfaceLogUtil.setSuccessLog(interfaceLog);
                if(jsonObject!=null) {
                    returnVal = jsonObject.getString("value");
                }
            }
        }
//            interfaceLogUtil.saveInterfaceLog(interfaceLog);
        return returnVal;
//        } catch (BaseException e) {
//            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
//            interfaceLogUtil.saveInterfaceLog(interfaceLog);
//            throw e;
//        } catch (Exception e) {
//            interfaceLogUtil.setErrorLog(interfaceLog, "同步异常");
//            interfaceLogUtil.saveInterfaceLog(interfaceLog);
//            throw new BaseException("油卡支付创建车队支付申请异常");
//        }
    }
}
